package com.ymatou.iapi.auth.testcase;

import java.security.PublicKey;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.auth.parameter.SignAuthBean;
import com.ymatou.iapi.auth.parameter.UserTokenDtoBean;
import com.ymatou.iapi.auth.service.SignAuthCall;
import com.ymatou.iapi.auth.service.UserTokenDtoCall;
import com.ymatou.iapi.login.service.LoginAuthCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymatou.iapi.userservice.parameter.req.CreateUserReq;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.core.tool.RSAUtils;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;

import com.ymt.utils.tag.P3;
import com.ymttest.business.service.AuthCallServiceV2;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.database.sqlwapper.LoginHistoryWapper;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * token认证 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SignAuth {
	private static SignAuthBean sab;
	private static SignAuthCall sac;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("token认证");
	}

	@Before
	public void caseUp() {
		sab = new SignAuthBean();
		sac = new SignAuthCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sac.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SignAuth_001() {
		Logger.start(true,"SignAuth认证token成功,验证useripauth记录");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			int UserId  = urrc.getUserid();
			
			String ip=regbean.getClientIp();
//			AuthCallService.authLogin(UserName, JUserCallService.defaultLoginPwd, "app", ip);
			Logger.comment("Token认证开始");
			sab.setToken(AccessToken);
			sab.setClientIp(ip);
			sab.setsClientType("MainSite");
			sab.setTokenSource("app");
			sab.setDeviceId(TestDataManager.getRandomUUID().toString());
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(UserId, sac.getUserId(), "验证UserId");
			Thread.sleep(1000);
			HashMap m = new HashMap();
			m.put("Uid", UserId);
			//TokenHistory现在延时写入 1小时1次，需要手工验证
			List<Map> list = new LoginHistoryWapper().selectTokenHistoryByUid(m);
			//记录sql在新的日志站写入 目前没有
//			Logger.verifyEquals(4, list.size(), "验证Ymt_TokenHistory表记录");
			for (Map map : list) {
				Logger.debug("LoginHistory:"+map);
			}
			Map useripauth=new UserSafeWapper().getIPAuthByUserIdAndArea(UserId, null, null, null);
			Logger.debug("useripauth:"+useripauth);
			//TODO SignAuth认证token成功,验证useripauth记录 现在手工验证
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SignAuth_002() {
		Logger.start(false,"输入错误token失败");
		try {
			sab.setToken("errtoken");
			sab.setsClientType("MainSite");
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(false, sac.getSuccess(), "验证Success");
			Logger.verifyEquals("未找到token", sac.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	// 配置了30s缓存，需要在服务端吧web.config 的<add key ="TokenCacheTimeOut" value
	// ="00:00:30"/> 注释掉才能跑通此case
	// @Test
	@Category(P3.class)
	@TestCase
	public void Tc_SignAuth_003() {
		Logger.start(true,"Token登入多次，数据库记录成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			int UserId  = urrc.getUserid();
			
			String ip=regbean.getClientIp();

			int count = 1000;
			double time = 0;
			for (int i = 0; i < count; i++) {
				sac = new SignAuthCall();
				sab = new SignAuthBean();
				sab.setToken(AccessToken);
				sab.setsClientType("AutoTest" + i);
				sac.setData(FormatUtil.beanToHttpString(sab, true, false));
				sac.callService();
				Logger.debug(sac.getReturnData());
				time += sac.getResponseTime();
			}
			Logger.debug("调用SignAuthCall " + count + "次耗时：" + time);
			Thread.sleep(1000);
			HashMap m = new HashMap();
			m.put("Uid", UserId);
			int dbcount = new LoginHistoryWapper().selectTokenHistoryCountByUid(m);
			Logger.verifyEquals(count, dbcount, UserId + ":验证Ymt_TokenHistory表记录count数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_SignAuth_004() {
		Logger.start(true, "Token登入");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			int UserId  = urrc.getUserid();
			Thread.sleep(100);
			String ip=regbean.getClientIp();
			sab.setToken(AccessToken);
			sab.setsClientType("MainSite");
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(UserId, sac.getUserId(), "验证UserId");
			Thread.sleep(1000);
			HashMap m = new HashMap();
			m.put("Uid", UserId);
			List<Map> list = new LoginHistoryWapper()
					.selectTokenHistoryByUid(m);
//			Logger.verifyEquals(4, list.size(), "验证Ymt_TokenHistory表记录");
			for (Map map : list) {
				Logger.debug(map);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	//该数据连接的是172.16.101.151
	public void Tc_SignAuth_005() {
		Logger.start(true, "App端，Token登入,日志表中会记录DeviceId");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			int UserId  = urrc.getUserid();
			
			String ip=regbean.getClientIp();
			sab.setToken(AccessToken);
			sab.setsClientType("app");
			sab.setDeviceId("TestApp");
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals("OK",sac.getJsonObject().get("Result").getAsJsonObject().get("Code").getAsString(), "验证ResultCode");
			Logger.verifyEquals(UserId,sac.getJsonObject().get("Result").getAsJsonObject().get("UserData").getAsJsonObject().get("UserId").getAsInt(), "验证UserId");
			Logger.verifyEquals(200,sac.getJsonObject().get("Code").getAsInt(), "验证Code");
			Logger.verifyEquals(true,sac.getJsonObject().get("Success").getAsBoolean(), "验证Success");
			Thread.sleep(2000);
			HashMap m = new HashMap();
			m.put("Uid", UserId);
			List<Map> list = new LoginHistoryWapper()
					.selectTokenHistoryByUid(m);
			boolean hasDeviceId=false;
			for (Map map : list) {
				if(map.get("DeviceId")!=null)
					Logger.verifyEquals("TestApp", map.get("DeviceId"), "验证设备ID存入Token表TestApp");
					hasDeviceId=true;
			}
			//JIRACONN-3571 设备不写token表
//			Logger.verifyEquals(true, hasDeviceId, "验证设备ID有存入Token表");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_SignAuth_006() {
		Logger.start(true,"SignAuth认证appid错误,失败");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			int UserId  = urrc.getUserid();
			
			String ip=regbean.getClientIp();
//			AuthCallService.authLogin(UserName, JUserCallService.defaultLoginPwd, "app", ip);
			Logger.comment("Token认证开始");
			sab.setToken(AccessToken);
			sab.setClientIp(ip);
			//需要配置文件配置无效应用
			sab.setsClientType("1");
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals("无效的应用来源:1", sac.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SignAuth_007() {
		Logger.start(true,"SignAuth的ip错误,不收集ip");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			int UserId  = urrc.getUserid();
			
			String ip=TestDataManager.getRandomUUID().toString();
//			AuthCallService.authLogin(UserName, JUserCallService.defaultLoginPwd, "app", ip);
			Logger.comment("Token认证开始");
			sab.setToken(AccessToken);
			sab.setClientIp(ip);
			sab.setsClientType("MainSite");
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_SignAuth_008() {
		Logger.start(true,"Token使用32位,失败");
		try {
			String ip=TestDataManager.getRandomIp();
			Logger.comment("Token认证开始");
			PublicKey publicKey = RSAUtils.loadPublicKey("yJlVOeahWIm8qv0Q65fyl58OVhyd3UrkwpfH2lyxsHvHzOJmaeP5BiPcG0/7wxdiqDvAFzYNrLdkgBfBcvyl9OzAOTV25wQPygRAzydfqclFP/Qsx1BnzBp/RvjNIyqKavaqpVijAAhaCfFE+Gv22FeT9PkFEYmuDK82cTIyYFs=");
			String fromatS=JUserCallService.defaultLoginPwd;
			String token = RSAUtils.encryptByPublicKey(fromatS, (RSAPublicKey) publicKey);
			sab.setToken(token);
			sab.setClientIp(ip);
			sab.setsClientType("MainSite");
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(false, sac.getSuccess(), "验证Success");
			Logger.verifyEquals("Token解密错误", sac.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SignAuth_009() {
		Logger.start(true,"SignAuth认证seller的token成功,验证selleronlinestatus记录");
		try {
			String type="seller.app.ymatou.com";
			String ip="127.0.0.1";
			 Map<String, Object> m = AuthCallServiceV2.authLogin("wesper", "123456", type, ip);
			Logger.debug(m);
			Logger.comment("Token认证开始");
			sab.setToken(m.get("accessToken").toString());
			sab.setClientIp(ip);
			sab.setsClientType(type);
			
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			UserSafeWapper wapper = new UserSafeWapper();
			Map<String,Object> m1=wapper.selectSelleronlineStatus(3383, "50001").get(0);
			String optime=m1.get("OpTime").toString();
			int difftime=YMTDateUtil.diffMinute(YMTDateUtil.getDate(optime.substring(0, 18),"yyyy-MM-dd HH:mm:ss"),YMTDateUtil.getDate());
			Logger.verifyNotNull(optime, "optime非空");
			Logger.verifyEquals(true,difftime<3, "difftime小于3分钟");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SignAuth_010() {
		Logger.start(true,"传入TokenSource为sellerapp用户注册成功JIRACONN-2123");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			int UserId  = urrc.getUserid();
			
			String ip=regbean.getClientIp();
//			AuthCallService.authLogin(UserName, JUserCallService.defaultLoginPwd, "app", ip);
			Logger.comment("Token认证开始");
			sab.setToken(AccessToken);
			sab.setClientIp(ip);
			sab.setsClientType("MainSite");
			sab.setTokenSource("sellerapp");
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(UserId, sac.getUserId(), "验证UserId");
			Thread.sleep(1000);
			HashMap m = new HashMap();
			m.put("Uid", UserId);
			List<Map> list = new LoginHistoryWapper().selectTokenHistoryByUid(m);
			//记录sql在新的日志站写入 目前没有
//			Logger.verifyEquals(4, list.size(), "验证Ymt_TokenHistory表记录");
			for (Map map : list) {
				Logger.debug(map);
			}
			Map useripauth=new UserSafeWapper().getIPAuthByUserIdAndArea(UserId, null, null, null);
			Logger.debug(useripauth);
			//TODO SignAuth认证token成功,验证useripauth记录 现在手工验证
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SignAuth_011() {
		Logger.start(true,"注册和认证传入TokenSource为app用户注册成功JIRACONN-2123");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setRegSource(1);
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			int UserId  = urrc.getUserid();
			
			String ip=regbean.getClientIp();
			sab.setToken(AccessToken);
			sab.setClientIp(ip);
			sab.setsClientType("MainSite");
			sab.setTokenSource("app");
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(UserId, sac.getUserId(), "验证UserId");
			Thread.sleep(1000);
			HashMap m = new HashMap();
			m.put("Uid", UserId);
			List<Map> list = new LoginHistoryWapper().selectTokenHistoryByUid(m);
			//记录sql在新的日志站写入 目前没有
//			Logger.verifyEquals(4, list.size(), "验证Ymt_TokenHistory表记录");
			for (Map map1 : list) {
				Logger.debug(map1);
			}
			Map useripauth=new UserSafeWapper().getIPAuthByUserIdAndArea(UserId, null, null, null);
			Logger.debug(useripauth);
			//TODO SignAuth认证token成功,验证useripauth记录 现在手工验证
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SignAuth_012() {
		Logger.start(true,"登入和认证传入TokenSource为app用户注册成功JIRACONN-2123");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setRegSource(1);
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			int UserId  = urrc.getUserid();
			
			String ip=regbean.getClientIp();
			
			Logger.comment("Token认证开始");
			sab.setToken(AccessToken);
			sab.setClientIp(ip);
			sab.setsClientType("MainSite");
			sab.setTokenSource("app");
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(UserId, sac.getUserId(), "验证UserId");
			Thread.sleep(1000);
			HashMap m = new HashMap();
			m.put("Uid", UserId);
			List<Map> list = new LoginHistoryWapper().selectTokenHistoryByUid(m);
			//记录sql在新的日志站写入 目前没有
//			Logger.verifyEquals(5, list.size(), "验证Ymt_TokenHistory表记录");
			for (Map map1 : list) {
				Logger.debug(map1);
			}
			Map useripauth=new UserSafeWapper().getIPAuthByUserIdAndArea(UserId, null, null, null);
			Logger.debug(useripauth);
			//TODO SignAuth认证token成功,验证useripauth记录 现在手工验证
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SignAuth_013() {
		Logger.start(true,"token小写兼容成功JIRACONN-2964");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setRegSource(1);
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			
			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String AccessToken=registerResult.get("Token").getAsString();
			int UserId  = urrc.getUserid();
			
			String ip=regbean.getClientIp();
			Logger.comment("Token认证开始");
			sab.setToken(AccessToken);
			sab.setClientIp(ip);
			sab.setsClientType("MainSite");
//			sab.setTokenSource("app");
			sab.setDeviceId(TestDataManager.getRandomUUID().toString());
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(UserId, sac.getUserId(), "验证UserId");
			Thread.sleep(1000);
			HashMap m = new HashMap();
			m.put("Uid", UserId);
			List<Map> list = new LoginHistoryWapper().selectTokenHistoryByUid(m);
			//记录sql在新的日志站写入 目前没有
//			Logger.verifyEquals(4, list.size(), "验证Ymt_TokenHistory表记录");
			for (Map map : list) {
				Logger.debug(map);
			}
			Map useripauth=new UserSafeWapper().getIPAuthByUserIdAndArea(UserId, null, null, null);
			Logger.debug(useripauth);
			//TODO SignAuth认证token成功,验证useripauth记录 现在手工验证
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	@Ignore
	//要开启来源配置才会记的，目前是不开启的，还没有做开启配置做不同来源区分的业务
	public void Tc_SignAuth_014() {
		Logger.start(true,"登入和认证传入TokenSource为sellerpc用户注册成功JIRACONN-3031");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setRegisterType(1);
			regbean.setTokenSource("sellerpc");
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = result.get("Token").getAsString();
			int UserId = urrc.getUserid();
//			Map map=AuthCallService.authLogin(regbean.getLoginId(), JUserCallService.defaultLoginPwd, "sellerpc", regbean.getClientIp(),"sellerpc");
			LoginAuthCall lg = JUserCallService.login(regbean.getLoginId(), JUserCallService.defaultLoginPwd,  "sellerpc", regbean.getClientIp(), regbean.getDeviceId());
//			Logger.verifyEquals(128, accessToken.length(), "验证新token长度");
			Logger.comment("Token认证开始");
			sab.setToken(lg.getUserData().getSign());
			sab.setClientIp(regbean.getClientIp());
			sab.setsClientType("MainSite");
			sab.setTokenSource("sellerpc");
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(UserId, sac.getUserId(), "验证UserId");
			Thread.sleep(1000);
			Logger.verifyEquals("sellerpc",new UserTokenWapper().getUserTokenByUid(UserId).get("tSource"),"验证tSource");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_SignAuth_015() {
		Logger.start(true,"登入和认证传入TokenSource为pc用户注册成功JIRACONN-3031");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setRegisterType(1);
			regbean.setTokenSource("pc");
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = result.get("Token").getAsString();
			int UserId = urrc.getUserid();
//			Map map=AuthCallService.authLogin(regbean.getLoginId(), JUserCallService.defaultLoginPwd, "sellerpc", regbean.getClientIp(),"sellerpc");
			LoginAuthCall lg = JUserCallService.login(regbean.getLoginId(), JUserCallService.defaultLoginPwd,  "pc", regbean.getClientIp(), regbean.getDeviceId());
			Logger.comment("Token认证开始");
			sab.setToken(lg.getUserData().getSign());
			sab.setClientIp(regbean.getClientIp());
			sab.setsClientType("MainSite");
			sab.setTokenSource("pc");
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(UserId, sac.getUserId(), "验证UserId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_SignAuth_016() {
		Logger.start(true,"SignAuth认证,验证ymatouusersafe.selleronlinestatus和useripauth ");
		try {
			String json=new UserTokenWapper().getUserTokenBytSource("app");
			String AccessToken=((Map)FormatUtil.GSONToObject(json, Map.class)).get("sId").toString();
			Object ouid=((Map)FormatUtil.GSONToObject(json, Map.class)).get("uId");
			Integer uid=new Double(ouid.toString()).intValue();
			UserSafeWapper usq=new UserSafeWapper();
			usq.deleteSellerOnlineStatus(uid);
			Logger.comment("Token认证开始");
			sab.setToken(AccessToken);
			sab.setClientIp("1.1.10.1"+new Random().nextInt(9));
			sab.setsClientType("seller.app.ymatou.com");
			sab.setTokenSource("app");
			sab.setDeviceId(TestDataManager.getRandomUUID().toString());
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Thread.sleep(1000);
			List<Map> lm = usq.selectSelleronlineStatus(uid, null);
			Logger.verifyEquals(1, lm.size(), "验证记录数据SelleronlineStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SignAuth_017() {
		Logger.start(true,"SignAuth认证token默认渠道成功");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			Thread.sleep(100);
			UserTokenDtoBean usertokendtoBean=new UserTokenDtoBean();
			UserTokenDtoCall usertokendtoCall=new UserTokenDtoCall();
			usertokendtoBean.setUserId(uid);
			usertokendtoCall.setData(usertokendtoBean);
			usertokendtoCall.callService();
			String AccessToken=usertokendtoCall.getResult();
			String ip=cur.getClientIp();
			Logger.comment("Token认证开始");
			sab.setToken(AccessToken);
			sab.setClientIp(ip);
			sab.setsClientType("MainSite");
//			sab.setTokenSource("app");
			sab.setDeviceId(TestDataManager.getRandomUUID().toString());
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(uid, sac.getUserId(), "验证UserId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SignAuth_018() {
		Logger.start(true,"SignAuth认证token默认渠道TokenSource使用非wxapp渠道,认证成功");
		//除了wxapp 其他只要有token 不匹配TokenSource都可以认证成功
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);

			UserTokenDtoBean usertokendtoBean=new UserTokenDtoBean();
			UserTokenDtoCall usertokendtoCall=new UserTokenDtoCall();
			usertokendtoBean.setUserId(uid);
			usertokendtoCall.setData(usertokendtoBean);
			usertokendtoCall.callService();
			String AccessToken=usertokendtoCall.getResult();
			String ip=cur.getClientIp();
			Logger.comment("Token认证开始");
			sab.setToken(AccessToken);
			sab.setClientIp(ip);
			sab.setsClientType("MainSite");
			sab.setTokenSource("app");
			sab.setDeviceId(TestDataManager.getRandomUUID().toString());
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(uid, sac.getUserId(), "验证UserId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SignAuth_019() {
		Logger.start(true,"SignAuth认证token默认渠道TokenSource使用wxapp渠道,认证失败");
		//除了wxapp 其他只要有token 不匹配TokenSource都可以认证成功
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			Thread.sleep(100);
			UserTokenDtoBean usertokendtoBean=new UserTokenDtoBean();
			UserTokenDtoCall usertokendtoCall=new UserTokenDtoCall();
			usertokendtoBean.setUserId(uid);
			usertokendtoCall.setData(usertokendtoBean);
			usertokendtoCall.callService();
			String AccessToken=usertokendtoCall.getResult();
			String ip=cur.getClientIp();
			Logger.comment("Token认证开始");
			sab.setToken(AccessToken);
			sab.setClientIp(ip);
			sab.setsClientType("MainSite");
			sab.setTokenSource("wxapp");
			sab.setDeviceId(TestDataManager.getRandomUUID().toString());
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(false, sac.getSuccess(), "验证Success");
			Logger.verifyEquals("未找到token", sac.getString("LastErrorMessage"), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_SignAuth_020() {
		Logger.start(true,"SignAuth认证token，wxapp渠道TokenSource使用wxapp渠道,认证成功");
		try {
			CreateUserReq cur = new CreateUserReq();
			cur = JUserCallService.initCreateUserBean(cur);
			int uid = JUserCallService.createUserCall(cur);
			Thread.sleep(100);
			UserTokenDtoBean usertokendtoBean=new UserTokenDtoBean();
			UserTokenDtoCall usertokendtoCall=new UserTokenDtoCall();
			usertokendtoBean.setUserId(uid);
			usertokendtoBean.setTokenSource("WxApp");
			usertokendtoCall.setData(usertokendtoBean);
			usertokendtoCall.callService();
			String AccessToken=usertokendtoCall.getResult();
			String ip=cur.getClientIp();
			Logger.comment("Token认证开始");
			sab.setToken(AccessToken);
			sab.setClientIp(ip);
			sab.setsClientType("MainSite");
			sab.setTokenSource("Wxapp");
			sab.setDeviceId(TestDataManager.getRandomUUID().toString());
			sac.setData(FormatUtil.beanToHttpString(sab, true, false));
			sac.callService();
			Logger.verifyEquals(true, sac.getSuccess(), "验证Success");
			Logger.verifyEquals(uid, sac.getUserId(), "验证UserId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
